/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "ui表格视口.h"
#include "UI/布局.h"


C_表格视口* C_表格视口::g当前操作表格视口 = nullptr;



static void on_表格视口布局(C_Widget* self, C_Widget** w, uint32 num) {
	f_布局_扩展视口纵向滚动(self->m_父部件, self, C_Widget::g_鼠标.lZ * 40);
}

static void on_表格视口绘制(C_Widget* self, S_2D画布* 画布) {
	C_表格视口* box = (C_表格视口*)self;

	box->m_背景绘制.m_属性 = f_vg_gen属性(画布);

	S_凸边图元 图元 = {};
	图元.属性 = f_vg_get属性指针(画布, box->m_背景绘制.m_属性);
	box->m_背景绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色填充);
	

	const auto& 字体 = f_surface_get字体库(*self->m_UIctx, S_UI主题::ui_默认字体);
	box->m_背景线绘制.f_Draw(画布, box->m_背景绘制.m_属性);
	box->m_文本绘制.f_Draw(画布, &字体, box->m_背景绘制.m_属性);

	S_2D顶点属性 属性 = { {1,1}, {1,1}, 1, 3 };
	f_vg_set属性(画布, box->m_背景绘制.m_属性, 属性);

	box->m_是否未绘制 = false;
}

static void on_表格视口变换修改(C_Widget* self) {
	C_表格视口* box = (C_表格视口*)self;


	S_2D顶点属性 属性 = { {1,1}, {0,0}, 1, 3 };
	属性.m_偏移 = {0, self->m_扩展视口属性->m_中心.y};
	属性.m_间隔 = uint32(box->m_Size.x)<<16 | uint32(box->m_Size.y);
	f_vg_set属性(self->m_画布, box->m_背景绘制.m_属性, 属性);

	box->m_背景绘制.f_alloc(2);
	mat3X2 mat;
	mat.row0 = {};
	mat.row2 = {};
	mat.row1 = {self->m_Size.x, 20};
	box->m_背景绘制.f_set单色填充(0, mat, S_UI主题::uic_页面焦点标签, S_Widget预设图元::m_矩形);

	mat.row1 = {60, self->m_Size.y};
	box->m_背景绘制.f_set单色填充(1, mat, S_UI主题::uic_页面焦点标签, S_Widget预设图元::m_矩形);
	


	uint32 列数量 = 0;
	uint32 行数量 = 0;
	uint32 最大数量 = 0;
	for (auto& e : box->m_绘制文本) {
		++列数量;
		行数量 += e.size();
	}
	box->m_背景线绘制.m_大小 = {80,20};
	box->m_背景线绘制.f_fill(列数量+2, 行数量+2);


	if (self->m_显示) {
		box->m_文本绘制.f_alloc(行数量, 20);
		uint32 index = 0;
		for (uint32 y = 0; y < 列数量; ++y) {
			行数量 = box->m_绘制文本[y].size();
			for (uint32 x = 0; x < 行数量; ++x) {
				mat.row0 = { float32(y * 80 + 80), float32(x * 20) };
				mat.row1 = { 80, 20 };
				box->m_文本绘制.f_设置文本({ mat.row0, mat.row1 }, S_UI主题::uic_文字颜色, index, box->m_绘制文本[y][x]);
				++index;
			}
		}
	}
	else {
		box->m_文本绘制.f_alloc(0, 20);
	}
	
}



C_表格视口::C_表格视口(S_UI创建环境 & ctx) : C_Widget(&ctx) {
	mf_布局Fun = on_表格视口布局;
	mf_DrawFun = on_表格视口绘制;
	mf_变换修改 = on_表格视口变换修改;

	m_是否未绘制 = true;
	g当前操作表格视口 = this;
}

C_表格视口::~C_表格视口() {
}

void C_表格视口::f_数据绘制() {
	if(m_被裁剪) return;

	//uint32 行 = 0;
	m_绘制文本.clear();
	auto 节点 = f_global_get激活节点();
	for (auto& node : 节点) {
		node->f_getStr(m_绘制文本);
		//++行;
	}

	m_绘制属性更新 = true;
}

C_Widget* f_工作区页面_创建表格视口(S_UI创建环境& ctx) {
	C_表格视口* ui表格视口 = new C_表格视口(ctx);

	f_widget_开启视口属性(ui表格视口);
	ui表格视口->m_扩展视口属性->m_大小 = { 2048, 2048 };

	auto* 滚动窗口 = f_ui_创建滚动框(ctx, true, true, ui表格视口);
	滚动窗口->mf_鼠标滚轮 = nullptr;

	C_表格视口::g当前操作表格视口 = ui表格视口;
	return 滚动窗口;
}
